﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CurriculumnManage.Helper;
using System.Configuration;
using CurriculumnManage.DBAccess;

namespace CurriculumnManage
{
    public partial class UpToGakuseki : Form
    {
        public UpToGakuseki()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if (!CommonFunction.CheckYear(this.txtYear))
                return;
            if(!CommonFunction.CheckGrade(this.cmbGrade))
                return;

            try
            {
                this.Button1.Enabled = false;
                this.Button2.Enabled = false;
                UpData();
                CommonFunction.ShowMsg("教育指針データ取込しました");
                this.Button1.Enabled = true;
                this.Button2.Enabled = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void UpData()
        {
            string targetDBConn = ConfigurationManager.ConnectionStrings["STR0100DB"].ConnectionString;
            string sourceDBConn = ConfigurationManager.ConnectionStrings["CurriculumnManageDB"].ConnectionString;
            int iNendo = MyUtils.ParseToInt(this.txtYear.Text, 0);
            int? iGrade = MyUtils.ParseToInt(this.cmbGrade.Text, 0);
            DBContext targetDB = new DBContext(targetDBConn);
            DBContext sourceDB = new DBContext(sourceDBConn);
            try
            {
                MyLog.TraceInstance.TraceInformation("upLoadData start:");
                targetDB.BeginTrans();
               // CommonFunction.CopyData(targetDB, sourceDB, "usp_tbSTRTeacher_get", "tbSTRTeacher", "usp_tbstrTeacher_add_edit");
               // CommonFunction.CopyData(targetDB, sourceDB, "usp_tbsTRTeacherDepartment_get", "tbstrTeacherDepartment", "usp_tbstrTeacherDepartment_add_edit");
                //CommonFunction.CopyData(targetDB, sourceDB, "usp_tbSTRMapTeacherDepartment_get", "tbSTRMapTeacherDepartment", "usp_webCurriCulumn_tbstrMapTeacherDepartment_add_edit");
               // CommonFunction.CopyData(targetDB, sourceDB, "usp_tbSTRSubjectGradeProfile_get", "tbSTRSubjectGradeProfile", "usp_tbSTRSubjectGradeProfile_add_edit", null, iNendo, iGrade == 0 ? null : iGrade);
               // CommonFunction.CopyData(targetDB, sourceDB, "usp_tbSTRSubjectProfile_get", "tbSTRSubjectProfile", "usp_tbSTRSubjectProfile_add_edit");
               // CommonFunction.CopyData(targetDB, sourceDB, "usp_tbSTRSubjectScheduleProfile_get", "tbSTRSubjectScheduleProfile", "usp_tbSTRSubjectScheduleProfile_add_edit");
                CommonFunction.CopyData(targetDB, sourceDB, "usp_tbSTRClass_get", "tbSTRClass", "usp_webCurriCulumn_tbSTRClass_add_edit");
                CommonFunction.CopyData(targetDB, sourceDB, "usp_tbSTRLectureTeacher_get", "tbSTRLectureTeacher", "usp_webCurriCulumn_tbSTRLectureTeacher_add_edit");
                targetDB.Commit();
            }
            catch (Exception ex)
            {
                targetDB.RollBack();
                throw ex;
            }
            finally
            {
                targetDB.Close();
                sourceDB.Close();
            }
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void UpToGakuseki_Load(object sender, EventArgs e)
        {
            CommonFunction.SetDefaultYear(this.txtYear);
        }
    }
}
